package com.larry.mr.flow;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @ProjectName map-reduce
 * @Author larry
 * @Date 10:34 2022/3/29
 * @Description TODO
 **/
public class FlowBeanMapper extends Mapper<LongWritable, Text,Text,FlowBean> {

    private Text k = new Text();
    FlowBean flowBean = new FlowBean();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        String[] fields = value.toString().split("\t");

        k.set(fields[1]);

        long upFlow = Long.parseLong(fields[fields.length - 3]);
        long downFlow = Long.parseLong(fields[fields.length - 2]);

        flowBean.setUpFlow(upFlow);
        flowBean.setDownFlow(downFlow);

        flowBean.setSum(upFlow,downFlow);

        context.write(k,flowBean);

    }
}
